
/****
 * Copyright (C) 2006 dNux Team
 *                                                            ****
 * This program is free software; you can redistribute it and/or *
 * modify under the terms of the GNU General Public License as   *
 * published by the Free Software Foundation;                    *
 * either version 2 of the License.                              *
 *                                                               *
 * This program is distributed in the hope that it will be       *
 * useful, but WITHOUT ANY WARRANTY; without even the implied    *
 * warranty of without even the implied waranty of               *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.          *
 * See the GNU General Public License for more details.          *
 *                                                               *
 * You should have received a copy of the                        *
 * GNU General Public License                                    *
 * along with this program; if not, write to the                 *
 * Free Software Foundation, Inc.,                               *
 * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA. *
 *                                                               *
 ****                                                         ****/

#include "dcore.h"

d_string_t
d_string_new(void) 
{
  d_string_t new;
  
  new = (d_string_t) malloc(sizeof(char));
  new[0] = '\0';
  
  return new;
}

void
d_string_free(d_string_t *string)
{
  if (*string != NULL) free(*string);
  *string = NULL;
}

d_bool_t
d_string_add_char(d_string_t *dest, char chr)
{
  d_string_t new;
  
  int len = strlen(*dest);
  new = (d_string_t) realloc(*dest, sizeof(char) * (len + 2));
  if ( new == NULL ) {
    return d_false;
  }

  *dest = new;
  (*dest)[len] = chr;
  (*dest)[len+1] = '\0';
  
  return d_true;
}

d_bool_t
d_string_add_string(d_string_t *dest, d_string_t src)
{
  int len;
  d_string_t temp;
  
  if (*dest == NULL) return d_false;
  if (src == NULL) return d_false;
  
  len = strlen(*dest) + strlen(src);

  temp = (d_string_t) realloc(*dest, sizeof(char) * (len + 1));
  if (temp == NULL) return d_false;
  *dest = temp;
  
  temp = strncat(*dest, src, sizeof(dest));
  if (temp == NULL) return d_false;
  
  return d_true;
}

d_bool_t
d_string_add_integer(d_string_t *dest, int integer)
{
  char temp[7];
  
  snprintf(temp, sizeof(temp), "%d", integer);
  
  if (d_string_add_string(dest, temp) == d_false) {
    return d_false;
  }
  return d_true;
}

d_string_t
d_string_format(d_string_t format, ...)
{
  va_list ap;
  
  d_string_t new;
  
  char chr;
  d_string_t str;
  int nbr;
  
  va_start(ap, format);
  
  new = d_string_new(); 
  
  while(*format != '\0') {
    if (*format != '%') {
      d_string_add_char(&new, *format);
      format++;	
      continue;
    }
		
    switch(*++format) {
    case '%':
      d_string_add_char(&new, '%');
      break;
    case 'd':
      nbr = va_arg(ap, int);
      d_string_add_integer(&new, nbr);
      break;
    case 'c':
      chr = va_arg(ap, int);
      d_string_add_char(&new, chr);
      break;
    case 's':
      str = va_arg(ap, char *);
      d_string_add_string(&new, str);
      break;
    }
    format++;
  }
  
  va_end(ap);
  
  return new;
}
